package tennisgame;

public class TennisGame
{

    private static final String[] scoreNames =
    {
        "love", "15", "30", "40"
    };
    private int playerOneScore;
    private int playerTwoScore;
    private String playerOneName;
    private String playerTwoName;

    public TennisGame(String playerOneName, String playerTwoName)
    {
        this.playerOneName = playerOneName;
        this.playerTwoName = playerTwoName;
    }

    public String getScore()
    {
        if ( hasWinner() )
        {
            return playerWithHigherScore() + " wins";
        }
        if ( hasAdvantage() )
        {
            return "advantage " + playerWithHigherScore();
        }
        if ( playerOneScore == playerTwoScore )
        {
            if ( playerOneScore >= 3 )
            {
                return "deuce";
            }
            return scoreNames[playerOneScore] + " all";
        }
        else
        {
            return scoreNames[playerOneScore] + " - " +
                    scoreNames[playerTwoScore];
        }
    }

    public void playerOneScores()
    {
        playerOneScore++;
    }

    public void playerTwoScores()
    {
        playerTwoScore++;
    }

    private boolean hasAdvantage()
    {
        return playerOneScore >= 3 && playerTwoScore >= 3 &&
                (playerOneScore == playerTwoScore - 1 ||
                playerTwoScore == playerOneScore - 1);
    }

    private String playerWithHigherScore()
    {
        if ( playerOneScore > playerTwoScore )
        {
            return playerOneName;
        }
        return playerTwoName;
    }

    private boolean hasWinner()
    {
        if ( (playerOneScore >= 4 && playerOneScore >= playerTwoScore + 2) ||
                (playerTwoScore >= 4 && playerTwoScore >= playerOneScore + 2) )
        {
            return true;
        }
        return false;
    }
}
